
	<body>
		<div class="bg">
			<div class="layui-bg-cyan"></div>
			<div class="layui-bg-white"></div>
		</div>
		<div class="layui-main layui-anim layui-anim-scaleSpring">
			<a href="index.html" class="logo ">
				<img src="<?=base_url()?>public/img/logo.png" />
			</a>
			<div class="login-box">
				<h1>用户注册</h1>
				<form class="layui-form layui-form-pane" action="">
					<div class="layui-form-item">
						<label class="layui-form-label">账号</label>
						<div class="layui-input-block">
							<input type="text" name="username" required lay-verify="required|username" placeholder="4-12位字母数字组合" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">邮箱</label>
						<div class="layui-input-block">
							<input type="text" name="email" required lay-verify="required|email" placeholder="请填写真实邮箱" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">部门</label>
						<div class="layui-input-block">
							<select name="department" lay-verify="required|number">
                                                            <option value="0">个人用户</option>
                                                            <?php foreach($deparment as $rows): ?>
                                                            <option value="<?=$rows['id']?>"><?=$rows['department_name']?></option>
                                                            <?php endforeach; ?>
							</select>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">密码</label>
						<div class="layui-input-block">
							<input type="password" name="password" id="password" required lay-verify="required|password" placeholder="6-12位字母数字,下划线" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">确认密码</label>
						<div class="layui-input-block">
							<input type="password" name="repassword" id="repassword" required lay-verify="required|password" placeholder="请再次输入密码" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-form-item register">
						<div class="layui-input-block">
							<button class="layui-btn layui-btn-danger" lay-submit lay-filter="register">注册</button>
						</div>
					</div>
				</form>
				<h4 class="tips">已有账号?请点击<a href="<?=site_url('user/login')?>" class="a">立即登录</a></h4>
			</div>
		</div>
	</body>

</html>

<script>
	layui.use(['form', 'layer'], function() {
		var form = layui.form,
			layer = layui.layer;
		form.verify({
			username:[/^[\d\w]{4,12}$/,'用户名必须6到12位，且不能出现空格'],
			password: [/^[\d\w_]{6,12}$/,'密码由6-12位字母数字,下划线组合而成']
		});
		form.on('submit(register)', function(data) {
			layer.open({
				type: 3,
				time: 1000,
				end: function() {
					let username = data.field.username;
					let email = data.field.email;
					let department = data.field.department;
					let password = data.field.password;
					let repassword = data.field.repassword;
                                        if(password!==repassword){
                                            layer.msg('输入的密码不一致');
                                            return false;
                                        }
                                        
					let jsonData = {
						'username': username,
						'email': email,
						'department': department,
						'password': password,
                                                'repassword':repassword
					};
					let url = "<?=site_url('user/register_do')?>";
					$.post(url, jsonData, function(redata) {
						if(redata.errno == '200') {
							layer.msg('注册成功,现在去登录!');
							setTimeout(function(){
								window.location.href = '<?=site_url('user/login')?>';
							},1000)
							
						} else {
							layer.msg(redata.error);
							return false;
						}
					}, 'json');  
				}
			});

			return false;
		});
	});
</script>